package com.bq.server.base;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bq.server.enumeration.YesOrNoEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Optional;


/**
 * service base 类
 *
 * @author: ChenSH
 * @date: Create by in 下午4:29 2018/5/2
 */
@Slf4j
public abstract class BaseService<MAPPER extends BaseMapper<TEntity>, TEntity>
        extends BaseServiceImpl<MAPPER, TEntity> {


    @SuppressWarnings("unchecked")
    @Override
    public boolean removeById(Serializable id) {
        //逻辑删除
        Class<TEntity> entityClass = this.currentModelClass();
        if (BaseEntity.class.isAssignableFrom(entityClass)) {
            BaseEntity<?> tEntity = (BaseEntity<?>) BeanUtils.instantiateClass(entityClass);
            tEntity.setDeleted(YesOrNoEnum.YES);
            tEntity.setId((Long) id);
            beforeUpdate((TEntity) tEntity);
            return SqlHelper.retBool(this.baseMapper.deleteByIdWithFill((TEntity) tEntity));
        }
        return super.removeById(id);
    }

    @Override
    protected String getNickname() {
//        Optional<LoginUserInfo> loginUserInfo = SsoUtils.loginUserInfo();
//        return loginUserInfo.map(LoginUserInfo::getNickname).orElse(null);
        return "";
    }
}
